package com.etoak.service;

import cn.hutool.json.JSONUtil;
import com.etoakk.common.bean.Email;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class EmailService {


    @Autowired
    JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;

    @RabbitListener(queues = "email")
    public void send(String message){
        System.out.println("收到消息："+message);
        Email email = JSONUtil.toBean(message, Email.class);
        SimpleMailMessage mailMessage=new SimpleMailMessage();
        mailMessage.setFrom(from);
        mailMessage.setTo(email.getReceiver());
        mailMessage.setSubject(email.getSubject());
        mailMessage.setText(email.getContent());
        System.out.println("开始发送邮件了");
        javaMailSender.send(mailMessage);
        System.out.println("发送结束");
    }
}
